package com.starsea.book.ui.newpage.viewmodel

import android.app.Application
import androidx.collection.arrayMapOf
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.starsea.book.BuildConfig
import com.starsea.book.base.BaseViewModel
import com.starsea.book.constant.AppConst
import com.starsea.book.help.http.newCallResponseBody
import com.starsea.book.help.http.okHttpClient
import com.starsea.book.help.http.postJson
import com.starsea.book.help.http.text
import com.starsea.book.ui.newpage.entity.AvtivityEntity
import com.starsea.book.ui.newpage.entity.BaseEntity
import com.starsea.book.ui.newpage.entity.LotteryInfoEntity
import com.starsea.book.ui.newpage.entity.SignBaseInfoEntity
import com.starsea.book.ui.newpage.entity.Today
import com.starsea.book.ui.newpage.entity.VersionInfoEntity
import com.starsea.book.ui.newpage.entity.WalletListDataEntity
import com.starsea.book.ui.newpage.entity.WelfareTaskListEntity
import com.starsea.book.ui.newpage.utils.gsonList
import com.starsea.book.ui.newpage.utils.gsonNormal
import com.starsea.book.ui.newpage.utils.gsonNormalNull
import com.starsea.book.utils.LogUtils
import com.starsea.book.utils.toastOnUi
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import kotlin.collections.set

class WelfareViewModel(application: Application) : BaseViewModel(application) {

    val dataListResult = MutableLiveData<BaseEntity<MutableList<WelfareTaskListEntity>>>()
    val signInfoResult = MutableLiveData<BaseEntity<SignBaseInfoEntity>>()
    val signResult = MutableLiveData<BaseEntity<Today>>()
    val signListResult = MutableLiveData<BaseEntity<SignBaseInfoEntity>>()
    val newUserListResult = MutableLiveData<BaseEntity<SignBaseInfoEntity>>()
    val newUserSignResult = MutableLiveData<BaseEntity<Today>>()
    val boxDetailsResult = MutableLiveData<BaseEntity<SignBaseInfoEntity>>()
    val openBoxResult = MutableLiveData<BaseEntity<SignBaseInfoEntity>>()
    val lotteryResult = MutableLiveData<BaseEntity<MutableList<LotteryInfoEntity>>>()
    val lotteryTiggerResult = MutableLiveData<BaseEntity<MutableList<LotteryInfoEntity>>>()
    val lotteryNumberCheckResult = MutableLiveData<BaseEntity<LotteryInfoEntity>>()
    val lotteryPanResult = MutableLiveData<BaseEntity<LotteryInfoEntity>>()
    val advertResult = MutableLiveData<BaseEntity<Today>>()
    val lotteryRecordResult = MutableLiveData<BaseEntity<MutableList<WalletListDataEntity>>>()
    val signHistoryResult = MutableLiveData<BaseEntity<String?>>()
    val checkActivityResult = MutableLiveData<BaseEntity<Boolean?>>()
    val getActivityResult = MutableLiveData<BaseEntity<AvtivityEntity?>>()
    val checkUpdateResult = MutableLiveData<BaseEntity<VersionInfoEntity?>>()


    fun getTaskList(
    ) {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareruleconfig/task/list"))
                    .get()

            }.text().let {

                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<MutableList<WelfareTaskListEntity>> =
                    it.gsonList(WelfareTaskListEntity::class.java)
                dataListResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun getSignBaseInfo(
    ) {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/signBaseInfo"))
                    .get()

            }.text().let {

                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<SignBaseInfoEntity> =
                    it.gsonNormal(SignBaseInfoEntity::class.java)
                signInfoResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun getBoxDetails(
    ) {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/openChestsInfo"))
                    .post("".toRequestBody())

            }.text().let {

                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<SignBaseInfoEntity> =
                    it.gsonNormal(SignBaseInfoEntity::class.java)
                boxDetailsResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun openBox(
    ) {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/openChestsReward"))
                    .post("".toRequestBody())

            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<SignBaseInfoEntity> =
                    it.gsonNormal(SignBaseInfoEntity::class.java)
                openBoxResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun sign(
    ) {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/signReward"))
                    .post("".toRequestBody())

            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<Today> =
                    it.gsonNormal(Today::class.java)
                signResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun signList(
    ) {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/signInfo"))
                    .get()

            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<SignBaseInfoEntity> =
                    it.gsonNormal(SignBaseInfoEntity::class.java)
                signListResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun lotteryList(
        drawType: Int
    ) {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfare-draw-prize/jackpot?drawType=$drawType"))
                    .post("".toRequestBody())

            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<MutableList<LotteryInfoEntity>> =
                    it.gsonList(LotteryInfoEntity::class.java)
                lotteryResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun lotteryNumberCheck(
        drawType: Int,
        isCheck: Boolean
    ) {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfare-draw-prize/draw_number_find?drawType=$drawType"))
                    .post("".toRequestBody())

            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<LotteryInfoEntity> =
                    it.gsonNormal(LotteryInfoEntity::class.java)
                entity.data?.isCheck = isCheck
                lotteryNumberCheckResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }


    fun lotteryPan(
    ) {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfare-draw-prize/turntable_draw"))
                    .post("".toRequestBody())
            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<LotteryInfoEntity> =
                    it.gsonNormal(LotteryInfoEntity::class.java)
                lotteryPanResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun lotteryTigger(
    ) {
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfare-draw-prize/seven_draw"))
                    .post("".toRequestBody())
            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<MutableList<LotteryInfoEntity>> =
                    it.gsonList(LotteryInfoEntity::class.java)
                lotteryTiggerResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }

    fun adverReward(
        data: String,
        type: Int
    ) {
//        类型 1、签到 2、开宝箱 3、答题卡 4、转盘 5、七连抽 6、补

        val map: MutableMap<String, Any> = arrayMapOf()
        map["data"] = data
        map["type"] = type
        val json = Gson().toJson(map)
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/adverReward"))
                    .postJson(json)

            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<Today> =
                    it.gsonNormal(Today::class.java)
                advertResult.postValue(entity)
//                {"status":"200","success":true,"msg":"操作成功","data":{"recordId":null,"customerId":"10","continuouKey":null,"amount":"1","amountType":"4","ruleType":"17","time":"0","operation":"1","date":null,"adver":null,"createId":null,"createTime":null,"updateId":null,"updateTime":null}}
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }


    }

    fun lotteryRedordReward(
        drawType: Int
    ) {
//       类型 1、转盘 2 七连抽

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfare-draw-prize/myPrize?drawType=$drawType"))
                    .post("".toRequestBody())

            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<MutableList<WalletListDataEntity>> =
                    it.gsonList(WalletListDataEntity::class.java)
                lotteryRecordResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }
    }


    fun getWeek(): MutableList<String> {

        val week: MutableList<String> = arrayListOf()
        week.add("一")
        week.add("二")
        week.add("三")
        week.add("四")
        week.add("五")
        week.add("六")
        week.add("日")
        return week
    }


    fun signHistory(
        date: String
    ) {
//        类型 1、签到 2、开宝箱 3、答题卡 4、转盘 5、七连抽 6、补

        val map: MutableMap<String, Any> = arrayMapOf()
        map["date"] = date
        val json = Gson().toJson(map)
        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/goldFillCheck"))
                    .postJson(json)
            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val baseEntity = BaseEntity<String?>()
                val objects = JsonParser.parseString(it).asJsonObject
                baseEntity.status = objects.get("status").asInt
                baseEntity.msg = objects.get("msg").asString
                signHistoryResult.postValue(baseEntity)

//                {"status":"200","success":true,"msg":"操作成功","data":{"recordId":null,"customerId":"10","continuouKey":null,"amount":"1","amountType":"4","ruleType":"17","time":"0","operation":"1","date":null,"adver":null,"createId":null,"createTime":null,"updateId":null,"updateTime":null}}
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }


    }


    fun getNewUser() {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/newPeopleWinInfo"))
            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<SignBaseInfoEntity> =
                    it.gsonNormal(SignBaseInfoEntity::class.java)
                newUserListResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }

    }

    fun newUserSign() {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/welfareReward/newPeopleReward"))
                    .post("".toRequestBody())
            }.text().let {
                LogUtils.e(">>>> ", "it = " + it)
                val entity: BaseEntity<Today> =
                    it.gsonNormal(Today::class.java)
                newUserSignResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }

    }

    fun checkActivity(type: String) {

        execute {
            okHttpClient.newCallResponseBody {

                val requestBody: RequestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("type", type.toString())
                    .build()
                url(AppConst.BASE_URL.plus("front-api/home/popupNoMoreReminders"))
                    .post(requestBody)
            }.text().let {
                LogUtils.e(">>>> activity ", "it = " + it)
                val baseEntity = BaseEntity<Boolean?>()
                val objects = JsonParser.parseString(it).asJsonObject
                baseEntity.status = objects.get("status").asInt
                baseEntity.msg = objects.get("msg").asString
                baseEntity.data = objects.get("data").asBoolean
                checkActivityResult.postValue(baseEntity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }

    }

    fun getActivity() {

        execute {
            okHttpClient.newCallResponseBody {
                url(AppConst.BASE_URL.plus("front-api/home/findEventNoticesOut"))
            }.text().let {
                LogUtils.e(">>>> activity ", "it = " + it)
                val entity: BaseEntity<AvtivityEntity?> =
                    it.gsonNormalNull(AvtivityEntity::class.java)
                getActivityResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }

    }


    fun checkUpdate() {

        execute {
            okHttpClient.newCallResponseBody {

                val requestBody: RequestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("version", BuildConfig.VERSION_NAME)
                    .build()
                url(AppConst.BASE_URL.plus("front-api/home/findNewVersion"))
                    .post(requestBody)
            }.text().let {
                LogUtils.e(">>>> activity ", "it = " + it)
                val entity: BaseEntity<VersionInfoEntity?> =
                    it.gsonNormalNull(VersionInfoEntity::class.java)
                checkUpdateResult.postValue(entity)
            }
        }.onError {
            context.toastOnUi(it.localizedMessage ?: "ERROR")
        }

    }

}
